﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DLM.Web.MVC.Extensions;
using DLM.Web.ViewModel.Admin;
using DLM.Services.Interfaces;
using DLM.Domain.Model;

namespace DLM.Web.MVC.Areas.Management.Controllers
{
    [AdminAuthorize]
    public class NoticeController : Controller
    {
        private IAdminService adminService;
        public NoticeController(IAdminService adminService)
        {
            this.adminService = adminService;
        }

        [HttpGet]
        public ActionResult Management(int? pageIndex)
        {
            NoticePageView pageView = new NoticePageView();
            pageView.Notices = this.adminService.Notices();
            pageView.Notice = new Notice();
            return View(pageView);
        }
        [HttpPost]
        public ActionResult Publish(FormCollection collection)
        {
            string subject = Server.HtmlEncode(collection["Notice.Subject"]);
            string content = Server.HtmlEncode(collection["Notice.Content"]);
            this.adminService.AddNotice(subject, content);
            return RedirectToAction("Management");
        }

        [HttpPost]
        public ActionResult Delete(int nid)
        {
            this.adminService.RemoveNotice(nid);
            return RedirectToAction("Management");
        }

    }
}
